<template>
	<el-dialog
	  v-model="dialog.visible"
	  :title="dialog.title"
	  destroy-on-close
	  :append-to-body="false"
	  width="550px"
	  class="editor-dialog-c"
	  @close="resetForm"
	  draggable
	  :close-on-click-modal="false"
	>
	
		<el-form
			ref="formRef"
			:model="formData"
			:rules="formRules"
			label-width="80px"
		>
			<el-form-item label="json数据" prop="content">
				<el-input v-model="formData.content" type="textarea" :rows="16" placeholder="请输入json数据" />
			</el-form-item>
		</el-form>
	
	<template #footer>
	  <div class="dialog-footer">
		<el-button color="#626aef" @click="submitForm"  :loading="loading">确 定</el-button>
		<el-button color="#1e1e1e" @click="closeDialog"  :loading="loading">取 消</el-button>
	  </div>
	</template>
	
	</el-dialog>
</template>

<script lang="ts" setup>
	import {
	  importThingmodelFuntParameter
	} from "@/api/sys/thingmodel";
	import type {FormRules } from 'element-plus'
	
	const loading = ref(false);
	const dialog = ref({
		visible:false,
		title:'',
	});
	const formRef=ref();
	const formData=ref<any>({
		id:'',
		type: '',
		content:''
	})
	
	const formRules=reactive<FormRules>({
		content: [{ required: true, message: "请输入json数据", trigger: "blur" }],
	});
	
	async function openDialog(row?: any, type?: string) {
		if('Input'==type){
			dialog.value.visible=true;
			dialog.value.title='输入参数';
			formData.value.id = row.id;
			formData.value.type = type;
		}else{
			dialog.value.visible=true;
			dialog.value.title='输出参数';
			formData.value.id = row.id;
			formData.value.type = type;
		}
	}
	
	const submitForm=()=>{
		formRef.value.validate((isValid: boolean) => {
		  if (isValid) {
				loading.value=true;
				importThingmodelFuntParameter(formData.value)
				.then((res:any) => {
					ElMessage.success(res.msg);
					closeDialog(formData.value.type,res?.data);
				})
				.catch((e:any) => {
					console.error(e)
				})
				.finally(() => {
					loading.value=false;
				});
		  }
		});
	}
	
	const emits = defineEmits(['closeDialog'])
	const closeDialog=(type:string,data:any)=>{
		dialog.value.visible=false;
		dialog.value.title="";
		emits('closeDialog',type,data)
		resetForm();
	}
	
	/**
	 * 重置表单
	 */
	function resetForm() {
		formRef.value.resetFields();
		formRef.value.clearValidate();
		formData.value.id = undefined;
		formData.value.type = undefined;
		formData.value.content = undefined;
	}	
	defineExpose({
	  openDialog,closeDialog
	});
	
	onMounted(() => {
	});
</script>

<style scoped>
	.custom-dialog{
		background-color: rebeccapurple;
	}
	
	:deep(.avatar-uploader .el-upload) {
	  border: 1px dashed #ccc;
	  border-radius: 6px;
	  cursor: pointer;
	  position: relative;
	  overflow: hidden;
	  width: 16rem;
	  height: 10rem;
	  
	}

	.avatar-uploader .el-upload:hover {
	  border-color: var(--el-color-primary);
	}
	
	.el-icon.avatar-uploader-icon {
	  font-size: 28px;
	  color: #8c939d;
	  width: 178px;
	  height: 178px;
	  text-align: center;
	}
	
	.thumbnail{
		height: 160px !important;
		width: 256px !important;
		-o-object-fit: fill;
		object-fit: fill;
	}
	
	.form-row{
		width: 100%;
		display: flex;
		flex-direction: row;
		padding-right: 0.625rem;;
	}
</style>